<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\CheckComment;
use App\Admin\Repositories\ArticleComment;
use App\Constants\MiniProgramConstant;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Support\Facades\DB;

class ArticleCommentController extends AdminController
{
    public function index(Content $content)
    {
        $tab = Tab::make();
        $tab->add('文章评论', $this->grid(), true);
        $tab->addLink('帖子评论', admin_route('topic_comments.index'));
        return $content->body($tab->withCard());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ArticleComment(['article.publisher', 'member']), function (Grid $grid) {
            if ($articleId = request('article_id')) {
                $grid->model()->where('article_id', $articleId);
            }

            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereExists(function ($q) use($map) {
                $q->select(DB::raw(1))
                    ->from('articles')
                    ->whereColumn('articles.id', 'article_comments.article_id')
                    ->whereIn('mini_type', array_keys($map));
            });

            $grid->model()->orderByDesc('created_at');
            $grid->column('created_at', '评论时间');
            $grid->column('article.mini_type', '小程序')->display(function ($miniType) use ($map) {
                return $miniType ? $map[$miniType] ?? '' : '';
            });
            $grid->column('member_id');
            $grid->column('article.publisher.name', '文章发布人');
            $grid->column('article_id');
            $grid->column('article.type', '文章类型')->using([
                1 => '热点快讯', 2 => '新闻资讯'
            ])->label([
                'primary', 'success'
            ]);
            $grid->column('content');
            $grid->column('status')->using(['未审核', '已通过', '未通过'])->label([
                'primary', 'success', 'danger',
            ]);;

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if ($actions->row->status == 0) {
                    $actions->append(new CheckComment(\App\Models\ArticleComment::class));
                }
            });

            $grid->disableFilterButton();
            $grid->disableRefreshButton();
            $grid->disableBatchDelete();
            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disableViewButton();
            $grid->disableEditButton();

            $grid->filter(function (Grid\Filter $filter) use ($map) {
                $filter->panel();
                $filter->expand();
                $filter->equal('article.mini_type', '小程序')->select($map)->width(2);
                $filter->equal('member_id')->width(2);
                $filter->like('article.publisher.name', '文章发布人')->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ArticleComment(), function (Show $show) {
            $show->field('id');
            $show->field('member_id');
            $show->field('nickname');
            $show->field('avatar');
            $show->field('article_id');
            $show->field('content');
            $show->field('fav_count');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ArticleComment(), function (Form $form) {
            $articleId = request('article_id');
            $form->display('id');
            $form->text('nickname');
            $form->image('avatar')->saveFullUrl()->uniqueName()->autoUpload();
            $form->hidden('status')->value('1');
            $form->hidden('article_id')->value($articleId);
            $form->textarea('content');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
